<template>
  <view class="cash-layout">
    <view class="cash-form">
        <view class="item acea-row row-between-wrapper">
          <view class="name">微信号</view>
          <view class="input">
            <input placeholder="请输入微信号" v-model="post.weixin" />
          </view>
        </view>
        <view class="item acea-row row-between-wrapper">
          <view class="name">提现金额</view>
          <view class="input">
            <input
              :placeholder="'最低提现金额' + minPrice"
              v-model="post.money"
            />
          </view>
          <div class="all-btn" @click="cashAll">全部</div>
        </view>
        <view class="tip">当前可提现金额: {{ commissionCount }}</view>
    </view>
    <view class="cash-btn">
      <view class="bnt" :class="post.money > 0 ? 'bnt-active' : 'bnt-disabed'" @click="submitted">提现</view>
    </view>

  </view>
</template>
<script>
import { getBank, postCashInfo } from '@/api/user'
import { required } from '@/utils/validate'
import { validatorDefaultCatch } from '@/utils/dialog'

export default {
  name: 'UserCash',
  components: {},
  props: {},
  data: function () {
    return {
      navList: [
        {
          name: '微信',
          type: 'weixin',
          icon: 'icon-weixin2',
        },
        // {
        //   name: '支付宝',
        //   type: 'alipay',
        //   icon: 'icon-icon34',
        // },
      ],
      post: {
        extract_type: 'weixin',
        alipay_code: '',
        money: '',
        name: '',
        bankname: '',
        cardnum: '',
        weixin: '',
      },
      currentTab: 0,
      minPrice: 0,
      banks: [],
      commissionCount: 0,
    }
  },
  mounted: function () {
    this.getBank()
  },
  methods: {
    cashAll(){
        if(this.commissionCount > 0){
          this.post.money = this.commissionCount
        }
    },
    getBank: function () {
      let that = this
      getBank().then(
        res => {
          that.banks = res.data.extractBank
          that.minPrice = res.data.minPrice
          that.commissionCount = res.data.commissionCount
        },
        function (err) {
          uni.showToast({
            title:
              err.msg || err.response.data.msg || err.response.data.message,
            icon: 'none',
            duration: 2000,
          })
        }
      )
    },
    async submitted() {
      let bankname = this.post.bankname,
        alipay_code = this.post.alipay_code,
        money = this.post.money,
        name = this.post.name,
        cardnum = this.post.cardnum,
        weixin = this.post.weixin,
        that = this
      // console.log(parseFloat(money))
      if (
        parseFloat(money) > parseFloat(that.commissionCount) ||
        parseFloat(that.commissionCount) == 0
      ) {
        uni.showToast({
          title: '余额不足',
          icon: 'none',
          duration: 2000,
        })
        return
      }
      if (parseFloat(money) < parseFloat(that.minPrice)) {
        uni.showToast({
          title: '最低提现金额' + that.minPrice,
          icon: 'none',
          duration: 2000,
        })
        return
      }
      //console.log(that.post.extract_type)
      switch (that.post.extract_type) {
        case 'alipay':
          try {
            await this.$validator({
              name: [required(required.message('支付宝用户名'))],
              alipay_code: [required(required.message('支付宝账号'))],
              money: [required(required.message('提现金额'))],
            }).validate({
              name,
              alipay_code,
              money,
            })
            let save = {
              extractType: that.post.extract_type,
              alipayCode: alipay_code,
              name: name,
              money: money,
            }
            that.save(save)
          } catch (e) {
            return validatorDefaultCatch(e)
          }
          break
        case 'weixin':
          try {
            await this.$validator({
              weixin: [required(required.message('提现微信号'))],
              money: [required(required.message('提现金额'))],
            }).validate({
              weixin,
              money,
            })
            let save = {
              extractType: that.post.extract_type,
              weixin: weixin,
              money: money,
            }
            that.save(save)
          } catch (e) {
            return validatorDefaultCatch(e)
          }
          break
      }
    },
    save: function (info) {
      postCashInfo(info).then(
        res => {
          uni.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000,
          })
          this.$yrouter.push({
            path: '/pages/user/promotion/CashAudit/index',
          })
        },
        err => {
          uni.showToast({
            title:
              err.msg || err.response.data.msg || err.response.data.message,
            icon: 'none',
            duration: 2000,
          })
        }
      )
    },
  },
}
</script>
<style lang="scss">
.cash-layout{
  height: 100%;
  display: flex;
  flex-direction: column;
  .cash-form{
    padding: 20rpx 0rpx;
    flex: 1;
    .item{
      padding: 20rpx 32rpx;
      border-bottom: 1px solid #eee;
      height: 1.07 * 100rpx;
      font-size: 0.3 * 100rpx;
      color: #333;
      background: #fff;
      &:last-child{
        border-bottom: none;
      }
    }
    .all-btn{
      color: #FF5900;
    }
    .name {
      width: 1.2 * 100rpx;
    }
    .input {
      flex: 1;
      padding: 0 20rpx;
    }
  }
  .tip {
    padding: 20rpx 32rpx;
    font-size: 0.26 * 100rpx;
    color: #999;
    margin-top: 0.25 * 100rpx;
  }
  .cash-btn{
    .bnt{
      font-size: 0.32 * 100rpx;
      color: #fff;
      width: 6.9 * 100rpx;
      height: 0.9 * 100rpx;
      text-align: center;
      border-radius: 0.5 * 100rpx;
      line-height: 0.9 * 100rpx;
      margin: 0.64 * 100rpx auto;
    }
    .bnt-disabed{
      background: #FF5900;
      border: 1rpx solid #FF5900;
      opacity: 0.3;
    }
    .bnt-active{
      background: #FF5900;
      border: 1rpx solid #FF5900;
    }
  }
}
</style>
